
import android.content.Context
import android.net.ConnectivityManager
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit

/**
 * 作者：马海钊
 * 时间：2023/8/14 15:29
 * 功能：
 */
class HttpUtils {
    private var httpUtils: HttpUtils? = null
    val apiService: ApiService

    init {
        val okHttpClient = OkHttpClient.Builder()
            //设置了连接超时时间、读取超时时间，并添加了一个网络拦截器用于记录HTTP请求和响应。
            .connectTimeout(50000, TimeUnit.MILLISECONDS)
            .readTimeout(50000, TimeUnit.MILLISECONDS)

            .addNetworkInterceptor(HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
            .build()

        val retrofit = Retrofit.Builder()
            .client(okHttpClient)
            .baseUrl("http://10.59.9.18/")
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .addConverterFactory(GsonConverterFactory.create())
            .build()

        apiService = retrofit.create(ApiService::class.java)
    }

//    @Synchronized
//    fun getInstance(): HttpUtils {
//        if (httpUtils == null) {
//            httpUtils = HttpUtils
//        }
//        return httpUtils!!
//    }

    fun isHttp(context: Context): Boolean {
        val conn = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
        val info = conn.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
        return info != null && info.isConnected
    }
}